local skel = fk.CreateSkill {
  name = "lb__yuqir",
  max_branches_use_time = function(self, player)
    local list =
    {
      ["drawcard"] = {
        [Player.HistoryRound] = 1
      },
    }
    local room = Fk:currentRoom()
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryTurn] = 1 }
    end
    return list
  end
}

Fk:loadTranslationTable {
  ["lb__yuqir"] = "狱契",
  [":lb__yuqir"] = "每轮限一次，当一名角色失去一个区域内最后的牌后，你可令其将手牌摸至体力上限，然后本轮每回合限一次，该角色使用牌后，你可以获得其一张牌。",

  ["#lb__yuqir_use"] = "狱契",
  ["@[player_chara]lb__yuqir-round"] = "狱契",
  ["#lb__yuqir"] = "狱契：你可以令 %dest 将手牌摸至体力上限",
  ["#lb__yuqir-ask"] = "狱契：你可以获得 %dest 一张牌",
  ["#lb__yuqir-choose"] = "狱契：获得 %dest 一张牌",

  ["$lb__yuqir1"] = "协定一旦成立，就要负责到底哦。",
  ["$lb__yuqir2"] = "以此为据，也就再无反悔的余地…你我都是。",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or player:usedSkillTimes("lb__yuqir", Player.HistoryRound, "drawcard") > 0 then return false end
    for _, move in ipairs(data) do
      if move.from then
        local p = move.from
        if p.dead then return end
        for _, info in ipairs(move.moveInfo) do
          for _, Area in ipairs({ Card.PlayerHand, Card.PlayerEquip, Card.PlayerJudge }) do
            if info.fromArea == Area and #p:getCardIds(Area) == 0 and p:getHandcardNum() < p.maxHp then
              event:setCostData(self, { tos = { p } })
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "#lb__yuqir::" .. event:getCostData(self).tos[1].id,
    })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "drawcard")
    local room = player.room
    local p = event:getCostData(self).tos[1]
    local n = p.maxHp - p:getHandcardNum()
    if n > 0 then
      p:drawCards(n, "lb__yuqir")
    end
    room:addTableMarkIfNeed(player, "@[player_chara]lb__yuqir-round", p.id)
  end,
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return not target.dead and not target:isNude()
        and player:hasSkill("lb__yuqir") and skel:withinBranchTimesLimit(player, tostring(target.id))
        and table.contains(player:getTableMark("@[player_chara]lb__yuqir-round"), target.id)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "#lb__yuqir-ask::" .. target.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, tostring(target.id))
    --player.room:useVirtualCard("slash", nil, player, target, skel.name, true)
    local room = player.room
    local card = room:askToChooseCard(player, {
      target = target,
      skill_name = skel.name,
      flag = "he",
      prompt = "#lb__yuqir-choose::" .. target.id,
    })
    room:obtainCard(player, card, false, fk.ReasonPrey, player, skel.name)
  end,
})

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[player_chara]lb__yuqir-round", 0)
end)

return skel
